#ifndef _DRAWING_H
#define _DRAWING_H

	void fillRoundRect(Graphics _g, int x, int y, int w, int h, int arcH, int arcW)
	{
		int lastY = 0;
	
		for(int i = arcH; i > 0; i--)
		{
			_g.fillRect(x + (arcW / (arcH - i + 1)), y + (arcH - i), w - (arcW / (arcH - i + 1)) * 2, 1);
			lastY = y + (arcH - i);
		}
		
		lastY++;
		
		_g.fillRect(x, lastY, w, h - arcH * 2);
		
		lastY--;
				
		for(int j = 0; j < arcH + 1; j++)
		{
			_g.fillRect(x + (arcW / (arcH - j + 1)), lastY + h - arcH * 2 + j, w - (arcW / (arcH - j + 1)) * 2, 1);
		}
	}
	
	void drawRoundButton(Graphics _g, int x, int y, int w, int h, int startColor, int endColor)
	{
		int rs = startColor >>>	16;
		int gs = (startColor >>> 8) & 0x000000FF;
		int bs = startColor & 0x000000FF;
				
		int rRatio = ((rs - (endColor >>> 16)) / (h / 4)) * -1;
		int gRatio = ((gs - ((endColor >>> 8) & 0x000000FF)) / (h / 4)) * -1;
		int bRatio = ((bs - (endColor & 0x000000FF)) / (h / 4)) * -1;
		
		for(int i = 0; i < h / 4; i++)
		{
			_g.setColor((rs<<16) | (gs<<8) | (bs));
			rs+=rRatio;
			gs+=gRatio;
			bs+=bRatio;
			
			fillRoundRect(_g, x + i, y + i, w - i * 2, h - i * 2, h * 10 / 100, w * 10 / 100);
		}
	}
	
	void drawRoundButton(Graphics _g, String str, int x, int y, int w, int h, int startColor, int endColor, int fontColor)
	{
		int rs = startColor >>>	16;
		int gs = (startColor >>> 8) & 0x000000FF;
		int bs = startColor & 0x000000FF;
				
		int rRatio = ((rs - (endColor >>> 16)) / (h / 4)) * -1;
		int gRatio = ((gs - ((endColor >>> 8) & 0x000000FF)) / (h / 4)) * -1;
		int bRatio = ((bs - (endColor & 0x000000FF)) / (h / 4)) * -1;
		
		for(int i = 0; i < h / 4; i++)
		{
			_g.setColor((rs<<16) | (gs<<8) | (bs));
			rs+=rRatio;
			gs+=gRatio;
			bs+=bRatio;
			
			fillRoundRect(_g, x + i, y + i, w - i * 2, h - i * 2, h * 10 / 100, w * 10 / 100);
		}
		
		font.setColor(fontColor);
		
		if(font.getStringWidth(str) < w)
		{
			font.drawString(str, x + w / 2, y + h / 2 + font.getFontHeight() / 2,DSmallFont.BOTTOM | DSmallFont.HCENTER);
		}
		else
		{
			String[] buffer = str.split(" ");
			int lines = buffer.length;
			int lastIndex = 0;
			
			for(int k = 0; k < buffer.length - 1; k++)
			{
				if(font.getStringWidth(buffer[lastIndex] + " " + buffer[k + 1]) < w)
				{
					buffer[lastIndex] = buffer[lastIndex] + " " + buffer[k + 1];
					lines--;
				}
				else
				{
					lastIndex++;
					buffer[lastIndex] = buffer[k + 1];
				}
			}
			
			for(int j = 0; j < lines; j++)
			{
				font.drawString(buffer[j], x + w / 2, y + (h / (lines+1)) + (h / (lines+1)) * j + font.getFontHeight() / 2, DSmallFont.BOTTOM | DSmallFont.HCENTER);
			}
		}
	}
	
	void drawGFRect(Graphics _g, int x, int y, int w, int h, int startColor, int endColor)
	{	
		int rs = startColor >>>	16;
		int gs = (startColor >>> 8) & 0x000000FF;
		int bs = startColor & 0x000000FF;
				
		int rRatio = ((rs - (endColor >>> 16)) / (h)) * -1;
		int gRatio = ((gs - ((endColor >>> 8) & 0x000000FF)) / (h)) * -1;
		int bRatio = ((bs - (endColor & 0x000000FF)) / (h)) * -1;
				
		for(int i = 0; i < h; i++)
		{
			_g.setColor((rs<<16) | (gs<<8) | (bs));
			_g.fillRect(x, y + i, w, 1);
			rs+=rRatio;
			gs+=gRatio;
			bs+=bRatio;
		}		
	}
	
	public static void drawSoftKeys(String lsk, String rsk)
	{
		#ifdef USE_SOFTKEYS_HUD
		font.getGraphics().setColor(MENUS.MAIN_MENU_BACKGROUND_COLOR);
		font.getGraphics().fillRect(0, SCREEN_HEIGHT - font.getFontHeight() - font.getFontHeight() / 5 - font.getPixelSize(), SCREEN_WIDTH, font.getFontHeight() + font.getFontHeight() / 5 + font.getPixelSize());
		#endif
	
		font.setColor(SOFTKEYS.COLOR);
		font.drawString(lsk, font.getFontWidth() / 2, SCREEN_HEIGHT - font.getFontHeight() / 5, DSmallFont.BOTTOM | DSmallFont.LEFT);
		font.drawString(rsk, SCREEN_WIDTH - font.getFontWidth() / 2, SCREEN_HEIGHT - font.getFontHeight() / 5, DSmallFont.BOTTOM | DSmallFont.RIGHT);
	}

	public static void drawSoftKeys(String lsk, int rsk)
	{
		#ifdef USE_SOFTKEYS_HUD
		font.getGraphics().setColor(MENUS.MAIN_MENU_BACKGROUND_COLOR);
		font.getGraphics().fillRect(0, SCREEN_HEIGHT - font.getFontHeight() - font.getFontHeight() / 5 - font.getPixelSize(), SCREEN_WIDTH, font.getFontHeight() + font.getFontHeight() / 5 + font.getPixelSize());
		#endif
	
		font.setColor(SOFTKEYS.COLOR);
		font.drawString(lsk, font.getFontWidth() / 2, SCREEN_HEIGHT - font.getFontHeight() / 5, DSmallFont.BOTTOM | DSmallFont.LEFT);
	}	
	
	public static void drawSoftKeys(int lsk, String rsk)
	{
		#ifdef USE_SOFTKEYS_HUD
		font.getGraphics().setColor(MENUS.MAIN_MENU_BACKGROUND_COLOR);
		font.getGraphics().fillRect(0, SCREEN_HEIGHT - font.getFontHeight() - font.getFontHeight() / 5 - font.getPixelSize(), SCREEN_WIDTH, font.getFontHeight() + font.getFontHeight() / 5 + font.getPixelSize());
		#endif
		
		font.setColor(SOFTKEYS.COLOR);
		font.drawString(rsk, SCREEN_WIDTH - font.getFontWidth() / 2, SCREEN_HEIGHT - font.getFontHeight() / 5, DSmallFont.BOTTOM | DSmallFont.RIGHT);
	}
	
	public static void drawAlphaRect(Graphics g, int x, int y, int w, int h, int color, int alphaLevel)
	{
		int[] rect = new int[w * h];
		
		for(int i = 0; i < rect.length; i++)
		{
			rect[i] = (alphaLevel << 24) | color;
		}
		
		g.drawRGB(rect, 0, w, x, y, w, h, true);
	}
	
	#ifdef WRAP_DRAWREGION
	void drawRegion(Image srcImg, int x_src, int y_src, int width, int height, int dummy, int x_dest, int y_dest, int dummy_2)
	{
		cGame_g.setClip(x_dest, y_dest, width, height);
		cGame_g.drawImage(srcImg, x_dest - x_src, y_dest - y_src, 0);
		cGame_g.setClip(0, 0, SCREEN_WIDTH, SCREEN_HEIGHT);
	}
	#endif
/*
	public static void drawPopupBox(DSmallFont font, String text, int x, int y, int w, int h, int padding, int textColor, int bgColor, int alphaLevel)
	{
		drawAlphaRect(font.getGraphics(), x, y, w, h, bgColor, alphaLevel);
		
		font.setColor(textColor);
		font.drawString(text, x + padding, y + padding, w - padding * 2, DSmallFont.TOP | DSmallFont.LEFT);
	}

	public static void drawStdPopupBox(DSmallFont font, String text)
	{
		drawAlphaRect(font.getGraphics(), SCREEN_WIDTH / 2 - font.getStringWidth(text, SCREEN_WIDTH - SCREEN_WIDTH / 5 * 3 - 10), SCREEN_HEIGHT / 2 - font.getStringHeight(text, SCREEN_WIDTH - SCREEN_WIDTH / 5 * 3 - 10), SCREEN_WIDTH / 5 * 3, font.getStringHeight(text, SCREEN_WIDTH - SCREEN_WIDTH / 5 * 3 - 10) + 10, 0x555555, 0x77);
		
		font.setColor(0xFFFFFF);
		font.drawString(text, SCREEN_WIDTH / 2, SCREEN_HEIGHT / 2, DSmallFont.HCENTER | DSmallFont.VCENTER);
	}
	*/
#endif // _DRAWING_H